<script type="text/javascript">
    
var <?= $this->jsObject ?> = {
	selGridId: "#<?= $this->selectedGrid->tableId ?>",
	gridId: "#<?= $this->allGrid->tableId ?>",
	addOpt: <?= $this->allGrid->invokeParams ?>,
	removeOpt: <?= $this->selectedGrid->invokeParams ?>
};

(function($, $f) {

	$.extend($f, {

		dialogOpt: {
            width:"226px"
        },

		addItem: function(evt, context) {
			var row = context["draggable"][0];
			var data = $($f.gridId).jqGrid("getRowData", row["id"]);
			if (data.id > 0) {
				var q = $.param({
					nodeid: $f.addOpt["nodeid"],
					id: data.id
				});
				$.ajax({
					url: $f.addOpt["url"] + "?" + q,
					success: function() {},
					error: function() {
						info_dialog("Ошибка", "Не удалось добавить атрибут", null, $f.dialogOpt);
					}
				});
			}
		},

		removeItem: function(evt, context) {
			var row = context["draggable"][0];
			var data = $($f.selGridId).jqGrid("getRowData", row["id"]);
			if (data.id > 0) {
				var q = $.param({
					nodeid: $f.removeOpt['nodeid'],
					id: data.id
				});
				$.ajax({
					url: $f.removeOpt['url'] + "?" + q,
					success: function() {},
					error: function() {
						info_dialog("Ошибка", "Не удалось удалить атрибут", null, $f.dialogOpt);
					}
				});
			}
		},

        gridComplete: function(categoryId, url) {
            $($f.selGridId).jqGrid('sortableRows', {
                update: function(event, ui) {
                    var index, id = parseInt(ui.item[0].id);
                    if (id > 0) {
                        index = $($f.selGridId).getInd(id);
                    }

                    if (index > 0) {
                       var q = $.param({id: categoryId, nodeid: id, index: index});
                       $.ajax({
                           url: url + '?' + q
                       });
                    }
                }
            });
        }
	});

})(jQuery, <?= $this->jsObject ?>);

</script>

<script type="text/javascript">
$(document).ready(function() {
	$("#save_button").click(function() {
		location.href = "<?= $this->backurl ?>";
	});
});
</script>
<?= $this->allGrid ?>
<br/>
<?= $this->selectedGrid ?>
<br/>
<? if ($this->backurl) : ?>
<p>
<?= $this->jQueryButton(
	'save_button',
	null,
	array(),
	array('title' => $this->backurlLabel, 'type' => 'button')
) ?>
</p>
<? endif ?>
